using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.CommerceServer.SecureSite
{
	public class CommerceAccount : Microsoft.CommerceServer.SecureSite.Wizard
	{
		private System.Windows.Forms.Label lblInstructions;
		private System.Windows.Forms.Label lblUserName;
		private System.Windows.Forms.Label lblPassword;
		private System.Windows.Forms.TextBox txtUserName;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Label lblExample;
		private System.Windows.Forms.Panel pnlAccount;
		private System.ComponentModel.IContainer components = null;

		public CommerceAccount()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.CommerceAccount_Load);
		}
		#endregion

		private void CommerceAccount_Load(object sender, System.EventArgs e)
		{
			this.lblExample = new System.Windows.Forms.Label();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.txtUserName = new System.Windows.Forms.TextBox();
			this.lblPassword = new System.Windows.Forms.Label();
			this.lblUserName = new System.Windows.Forms.Label();
			this.lblInstructions = new System.Windows.Forms.Label();
			this.pnlAccount = new System.Windows.Forms.Panel();
			this.pnlAccount.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnNext
			// 
			this.btnNext.Name = "btnNext";
			// 
			// btnPrevious
			// 
			this.btnPrevious.Name = "btnPrevious";
			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
			// 
			// lblExample
			// 
			this.lblExample.Location = new System.Drawing.Point(272, 144);
			this.lblExample.Name = "lblExample";
			this.lblExample.Size = new System.Drawing.Size(160, 23);
			this.lblExample.TabIndex = 0;
			this.lblExample.Text = "Example (Domain\\UserName)";
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(272, 208);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(160, 20);
			this.txtPassword.TabIndex = 1;
			this.txtPassword.Text = "";
			// 
			// txtUserName
			// 
			this.txtUserName.Location = new System.Drawing.Point(272, 176);
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new System.Drawing.Size(160, 20);
			this.txtUserName.TabIndex = 0;
			this.txtUserName.Text = "";
			// 
			// lblPassword
			// 
			this.lblPassword.Location = new System.Drawing.Point(152, 208);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.TabIndex = 0;
			this.lblPassword.Text = "Password:";
			// 
			// lblUserName
			// 
			this.lblUserName.Location = new System.Drawing.Point(152, 176);
			this.lblUserName.Name = "lblUserName";
			this.lblUserName.TabIndex = 0;
			this.lblUserName.Text = "User Name:";
			// 
			// lblInstructions
			// 
			this.lblInstructions.Location = new System.Drawing.Point(40, 16);
			this.lblInstructions.Name = "lblInstructions";
			this.lblInstructions.Size = new System.Drawing.Size(480, 104);
			this.lblInstructions.TabIndex = 0;
			// 
			// pnlAccount
			// 
			this.pnlAccount.Controls.Add(this.lblExample);
			this.pnlAccount.Controls.Add(this.txtPassword);
			this.pnlAccount.Controls.Add(this.txtUserName);
			this.pnlAccount.Controls.Add(this.lblPassword);
			this.pnlAccount.Controls.Add(this.lblUserName);
			this.pnlAccount.Controls.Add(this.lblInstructions);
			this.pnlAccount.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlAccount.Location = new System.Drawing.Point(0, 72);
			this.pnlAccount.Name = "pnlAccount";
			this.pnlAccount.Size = new System.Drawing.Size(552, 262);
			this.pnlAccount.TabIndex = 4;
			// 
			// CommerceAccount
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 374);
			this.Controls.Add(this.pnlAccount);
			this.Name = "CommerceAccount";
			this.Controls.SetChildIndex(this.pnlAccount, 0);
			this.pnlAccount.ResumeLayout(false);
			this.ResumeLayout(false);

            // fill in the username
            this.txtUserName.Text = Environment.UserDomainName + @"\" + Environment.UserName;
			CommerceSecurityInfo.Log("FORM: CommerceAccount form loaded");
			base.Title = "Create User Account";
			this.lblInstructions.Text = "Security wizard will create a new account. If the account already exists it will verify username and password. The credentials will be used to secure file system and Commerce COM+ objects. Click Next to continue.";
		}

		protected override void btnPrevious_Click(object sender, System.EventArgs e)
		{
			CommerceSecurityInfo.MoveToForm("CommerceSite");
			base.Visible = false;
		}

		private void validateForm()
		{
			ValidateTextBox(this.txtUserName, "You must provide a UserName");
			ValidateTextBox(this.txtPassword, "You must provide a Password");
		}
		
		private void ValidateTextBox(TextBox txtBox, string message)
		{
			TextBox box1 = txtBox;
			if (box1.Text == string.Empty)
			{
				this.MasterErrorProvider.SetError(box1, message);
			}
			else
			{
				this.MasterErrorProvider.SetError(box1, "");
			}
		}

		protected override void btnNext_Click(object s, EventArgs e)
		{
			validateForm();
			if (base.IsValid)
			{
				CommerceSecurityInfo.Username = this.txtUserName.Text.Substring(this.txtUserName.Text.IndexOf(@"\") + 1);
				CommerceSecurityInfo.Password = this.txtPassword.Text;
				CommerceSecurityInfo.Domain = this.txtUserName.Text.Substring(0, this.txtUserName.Text.IndexOf(@"\"));
				CommerceSecurityInfo.MoveToForm("IISApplicationPool");
				base.Visible = false;
			}
		}
	}
}

